package com.mes.schedule.manager;

import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.mes.common.exception.bo.BaseBusinessException;
import com.mes.common.exception.db.BaseDBException;
import com.mes.common.manager.BasicManager;
import com.mes.common.model.ObjectOperation;
import com.mes.common.spring.SpringUtils;
import com.mes.common.util.IdentityGenerator;
import com.mes.common.util.Page;
import com.mes.common.util.SysUtil;
import com.mes.schedule.ScheduleOperation.domain.OperationGroupTasks;
import com.mes.schedule.ScheduleOperation.domain.OperationOperationTask;
import com.mes.schedule.ScheduleOperation.domain.OperationPartTask;
import com.mes.schedule.ScheduleOperation.domain.OperationSplitBatch;
import com.mes.schedule.algorithm.ScheduleStateFind;
import com.mes.schedule.dao.ScheduleOperationRecordDao;
import com.mes.schedule.domain.SOperationTask;
import com.mes.schedule.domain.SPartTask;
import com.mes.schedule.domain.ScheduleEvent;
import com.mes.schedule.domain.ScheduleOperationRecord;
import com.mes.schedule.domain.ScheduleScheme;
import com.mes.schedule.manager.ScheduleOperation.OperationGroupTasksManager;
import com.mes.schedule.manager.ScheduleOperation.OperationOperationTaskManager;
import com.mes.schedule.manager.ScheduleOperation.OperationPartTaskManager;
import com.mes.schedule.manager.ScheduleOperation.OperationSplitBatchManager;
import com.mes.schedule.manager.iface.IScheduleOperationRecordManager;
import com.mes.schedule.util.StringOper;

/**
 * 最终应该由java消息系统来决替代，而不是数据库
 * 
 * @author AutoGenerateJavaTool 1.0
 * 
 */
@Component("scheduleOperationRecordManager")
@Transactional
public class ScheduleOperationRecordManager	extends BasicManager implements IScheduleOperationRecordManager{
	private final static Log log = LogFactory
			.getLog(ScheduleOperationRecordManager.class);
	 
	@Resource
	public ScheduleOperationRecordDao scheduleOperationRecordDao;
	@Resource
	private ScheduleStateFind scheduleStateFind = new ScheduleStateFind();
	@Resource
	public OperationSplitBatchManager operationSplitBatchManager;
	@Resource
	private OperationPartTaskManager operationPartTaskManager;
	@Resource
	private OperationGroupTasksManager operationGroupTasksManager;
	@Resource
	private OperationOperationTaskManager operationOperationTaskManager;

	 
	public void SaveOrUpdateBySource(String masterShopUid,
			ScheduleEvent scheduleEvent) throws BaseBusinessException {
		// ScheduleSchemeManager scheduleSchemeManager = SpringUtils
		// .getBean("schemeManager");
		// Set<String> schemeUidSet = scheduleSchemeManager
		// .getSchemeUidSet(masterShopUid);
		// if (schemeUidSet == null) {
		// return;
		// }
		//
		// try {
		//
		// for (String scheduleUid : schemeUidSet) {
		// scheduleEvent.setScheduleUid(scheduleUid);
		// ScheduleEvent oldScheduleEvent = scheduleEventDao
		// .getUnHandleEvent(scheduleEvent);
		// if (oldScheduleEvent == null) {
		// this.add(scheduleEvent);
		// } else {
		// String eventUid = oldScheduleEvent.getScheduleEventUid();
		// ObjectOperation.copyAttributes(scheduleEvent,
		// oldScheduleEvent);
		// oldScheduleEvent.setScheduleEventUid(eventUid);
		// this.update(oldScheduleEvent);
		// }
		// }
		// } catch (BaseDBException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// throw new BaseBusinessException(e.getMessage());
		// }

	}

	 
	public void addScheduleEvent(String masterShopUid,
			ScheduleEvent scheduleEvent) throws Exception {
		ScheduleSchemeManager scheduleSchemeManager = SpringUtils
				.getBean("schemeManager");
		Set<String> schemeUidSet = scheduleSchemeManager
				.getSchemeUidSet(masterShopUid);
		if (schemeUidSet == null) {
			return;
		}

		try {

			for (String scheduleUid : schemeUidSet) {
				//
				try {
					// 要clone,否则add之后就会变成hibermate持久态，认为是一个对象
					ScheduleEvent e = (ScheduleEvent) scheduleEvent.clone();
					e.setScheduleUid(scheduleUid);
					this.add(e);
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} catch (BaseDBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BaseBusinessException(e.getMessage());
		}

	}

	public void writeOperLog(ScheduleScheme scheme, String eventType,
			Element operElement) throws Exception {

		// 写日志
		ScheduleEvent scheduleEvent = new ScheduleEvent();
		scheduleEvent.setScheduleUid(scheme.getScheduleUid());
		scheduleEvent.setEventType(eventType);
		// scheduleEvent.setKeyUid(keyUid);
		Format format = Format.getCompactFormat();
		format.setEncoding("UTF-8");
		format.setIndent("  ");
		XMLOutputter outputter = new XMLOutputter(format);
		scheduleEvent.setEventContent(outputter.outputString(operElement));

		scheduleEvent.setEventState(ScheduleEvent.EVENTSTATE_SUCCESS);
		scheduleEvent.setSourceUid(scheme.getScheduleUid());
		scheduleEvent.setCreatorUid(SysUtil.getSessionEmployeeUid());
		scheduleEvent.setCreateTime(new Date());
		String str = "";
		try {
			SAXBuilder sb = new SAXBuilder(false);
			String strXml = scheduleEvent.getEventContent();
			StringReader read = new StringReader(strXml);
			Document doc = sb.build(read);
			if (eventType.equals(ScheduleEvent.EVENTTYPE_PARTTASK_PLANINFO)) {
				str = scheduleStateFind.eventProPartTaskPlanInfoChange(scheme,
						scheduleEvent, doc);
			} else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_PARTTASK_SPLITBATCH))
				str = scheduleStateFind.eventProPartTaskSplitBatch(scheme,
						scheduleEvent, doc);
			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_OPERATIONTASK_PLANINFO))
				str = scheduleStateFind.eventProOperationPlanInfo(scheme,
						scheduleEvent, doc);
			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_PARTTASK_COMBINEBATCH))
				str = scheduleStateFind.eventProPartTaskCombineBatch(scheme,
						scheduleEvent, doc);
			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_PARTTASK_CANCELCOMBINEBATCH))
				str = scheduleStateFind.eventProPartTaskCancelCombineBatch(
						scheme, scheduleEvent, doc);
			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_OPERATIONTASK_GROUP))
				str = scheduleStateFind.eventProOperationTaskGroup(scheme,
						scheduleEvent, doc);

			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_OPERATIONTASK_CANCELGROUP))
				str = scheduleStateFind.eventProOperationTaskCancelGroup(
						scheme, scheduleEvent, doc);

			else if (eventType.equals(ScheduleEvent.EVENTTYPE_PARTTASK_ADD))
				str = scheduleStateFind.eventProPartTaskAdd(scheme,
						scheduleEvent, doc);

			else if (eventType.equals(ScheduleEvent.EVENTTYPE_PARTTASK_DELETE))
				str = scheduleStateFind.eventProPartTaskDELETE(scheme,
						scheduleEvent, doc);

			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_OPERATIONTASK_ADD))
				str = scheduleStateFind.eventProOperationTaskAdd(scheme,
						scheduleEvent, doc);

			else if (eventType
					.equals(ScheduleEvent.EVENTTYPE_OPERATIONTASK_DELETE))
				str = scheduleStateFind.eventProOperationTaskDELETE(scheme,
						scheduleEvent, doc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scheduleEvent.setEventDesc(str);
		this.add(scheduleEvent);
	}

	 
	public List<String> getEventChildPara(ScheduleEvent scheduleEvent,
			String eventPara, ScheduleScheme scheme) throws Exception {
		List<String> paraValueStringList = new ArrayList<String>();
		SAXBuilder sb = new SAXBuilder(false);
		SimpleDateFormat formatDate = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		BeanUtils beanUtils = new BeanUtils();
		String strXml = scheduleEvent.getEventContent();
		StringReader read = new StringReader(strXml);
		Document doc = sb.build(read);
		String eventType = scheduleEvent.getEventType();
		Element rootElement = doc.getRootElement();
		List<Element> elements = rootElement.getChildren();
		List<SPartTask> partTasks = new ArrayList<SPartTask>();
		for (Element elem : elements) {
			String paraValueString = elem.getAttributeValue(eventPara);
			if (paraValueString != null && !"".equals(paraValueString)) {
				paraValueStringList.add(paraValueString);
			}
		}
		return paraValueStringList;
	}

	 
	public String getEventPara(ScheduleEvent scheduleEvent, String eventPara,
			ScheduleScheme scheme) throws Exception {
		String paraValueString = "";
		SAXBuilder sb = new SAXBuilder(false);
		SimpleDateFormat formatDate = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		BeanUtils beanUtils = new BeanUtils();
		String strXml = scheduleEvent.getEventContent();
		StringReader read = new StringReader(strXml);
		Document doc = sb.build(read);
		String eventType = scheduleEvent.getEventType();
		Element rootElement = doc.getRootElement();
		paraValueString = rootElement.getAttributeValue(eventPara);
		return paraValueString;
	}

	 
	public List<ScheduleOperationRecord> findScheduleEventsByConditions(
			ScheduleScheme scheme, String parentUid, Date fromDate,
			Date toDate, Integer eventState, String eventType,
			String eventObject, String drawingId, String batchNum, Page page)
			throws Exception {

		SAXBuilder sb = new SAXBuilder(false);
		StringBuilder strResult = new StringBuilder();
		strResult.append("发生如下事件：\n");
		int n = 0;

		List<ScheduleOperationRecord> scheduleEventList = (List<ScheduleOperationRecord>) scheduleOperationRecordDao
				.getAllEvents(scheme.getScheduleUid(), fromDate, toDate,
						eventState, eventType, page);// 获得近两天的事件
		List<ScheduleOperationRecord> scheduleEventListForReturnList = new ArrayList<ScheduleOperationRecord>();
		if (scheduleEventList != null && scheduleEventList.size() > 0) {
			for (ScheduleOperationRecord scheduleEvent : scheduleEventList) {

				// if(!parentUid.equals("")){
				// if (!parentUid.equals(scheduleEvent.getParentUid())) {
				// continue;
				// }
				// }
				if (parentUid != null
						&& !parentUid.equals(scheduleEvent.getParentUid())) {
					if (parentUid.equals("")) {
						if (scheduleEvent.getParentUid() != null) {
							continue;
						}
					} else {
						if (!parentUid.equals(scheduleEvent.getParentUid())) {
							continue;
						}
					}
				}
				if (fromDate != null
						&& scheduleEvent.getCreateTime().before(fromDate)) {
					continue;
				}
				if (toDate != null
						&& scheduleEvent.getCreateTime().after(toDate)) {
					continue;
				}
				if (eventState != null
						&& scheduleEvent.getEventState() != eventState) {
					continue;
				}
				if (eventObject != null && eventObject.equals("operationTask")) {
					if (scheduleEvent.getEventType().equals(
							ScheduleEvent.EVENTTYPE_PARTTASK_ADD)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_PARTTASK_CANCELCOMBINEBATCH)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_PARTTASK_CANCELSPLITBATCH)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_PARTTASK_COMBINEBATCH)
							|| scheduleEvent.getEventType().equals(
									ScheduleEvent.EVENTTYPE_PARTTASK_DELETE)
							|| scheduleEvent.getEventType().equals(
									ScheduleEvent.EVENTTYPE_PARTTASK_PLANINFO)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_PARTTASK_SPLITBATCH)
							|| scheduleEvent.getEventType().equals(
									ScheduleEvent.EVENTTYPE_PARTTASK_ADD)) {
						continue;
					}
				}
				if (eventObject != null && eventObject.equals("partTask")) {

					if (scheduleEvent.getEventType().equals(
							ScheduleEvent.EVENTTYPE_OPERATIONTASK_ADD)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_OPERATIONTASK_CANCELGROUP)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_OPERATIONTASK_DELETE)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_OPERATIONTASK_GROUP)
							|| scheduleEvent
									.getEventType()
									.equals(
											ScheduleEvent.EVENTTYPE_OPERATIONTASK_PLANINFO)) {
						continue;
					}
				}
				if (eventType != null && !eventType.equals("")
						&& !scheduleEvent.getEventType().equals(eventType)) {
					continue;
				}

				if (drawingId != null
						&& !drawingId.equals("")
						&& !StringOper.equals(drawingId, scheduleEvent
								.getDrawingId())) {
					continue;
				}
				if (batchNum != null
						&& !batchNum.equals("")
						&& !StringOper.equals(batchNum, scheduleEvent
								.getBatchNum())) {
					continue;
				}
				scheduleEventListForReturnList.add(scheduleEvent);
			}
		}
		if (scheduleEventListForReturnList != null
				&& scheduleEventListForReturnList.size() > 0) {
			Collections.sort(scheduleEventListForReturnList,
					new Comparator<Object>() {
						public int compare(Object o1, Object o2) {
							ScheduleOperationRecord scheduleEvent1 = (ScheduleOperationRecord) o1;
							ScheduleOperationRecord scheduleEvent2 = (ScheduleOperationRecord) o2;
							int i = 0;
							if (scheduleEvent1.getCreateTime().before(
									scheduleEvent2.getCreateTime())) {
								i = 1;
							}
							return i;
						}
					});
		}

		return scheduleEventListForReturnList;
	}

	public ScheduleOperationRecordDao getScheduleOperationRecordDao() {
		return scheduleOperationRecordDao;
	}

	public void setScheduleOperationRecordDao(
			ScheduleOperationRecordDao scheduleOperationRecordDao) {
		this.scheduleOperationRecordDao = scheduleOperationRecordDao;
	}

	 
	public void createSplitBatchInfo(List<SPartTask> updatedPartList,
			List<SOperationTask> splitedOperationsList, String parentUid) {
		if (updatedPartList == null || splitedOperationsList == null) {
			return;
		}
		try {
			for (SPartTask partTask : updatedPartList) {
				ScheduleOperationRecord record = new ScheduleOperationRecord();
				record.setScheduleOperationUid(IdentityGenerator
						.GetIdentityString());
				record.setDrawingId(partTask.getDrawingId());
				record.setBatchNum(partTask.getBatchNum());
				record.setScheduleUid(partTask.getScheduleUid());
				record.setEventState(1);
				record.setParentUid(parentUid);
				record.setCreateTime(new Date());
				record
						.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH);
				this.add(record);
				List<OperationSplitBatch> splitBatchList = new ArrayList<OperationSplitBatch>();

				for (SOperationTask opTask : splitedOperationsList) {
					OperationSplitBatch splitBatch = new OperationSplitBatch();
					splitBatch.setScheduleOperationUid(IdentityGenerator
							.GetIdentityString());
					splitBatch.setScheduleUid(partTask.getScheduleUid());
					splitBatch.setDrawingId(partTask.getDrawingId());
					splitBatch.setOriginalBatchNum(partTask.getBatchNum());
					splitBatch.setNewBatchNum(opTask.getBatchNum());
					splitBatch.setParentUniqueId(record
							.getScheduleOperationUid());
					splitBatch.setSplitNum(opTask.getPlanQty());
					splitBatch.setStartOperaton(opTask.getOperationIdDesc());
					splitBatchList.add(splitBatch);
				}
				this.operationSplitBatchManager.add(splitBatchList);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		// TODO Auto-generated method stub
	}

	public Element createElementFormat(ScheduleOperationRecord record,
			Map<String, SPartTask> allPartTaskMap,
			Map<String, SOperationTask> allOperationTaskMap) {
		String drawingId = record.getDrawingId();

		String returnString = "";
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH)) {

			String batchNum = record.getBatchNum();
			String operationIdDesc = record.getOperationIdDesc();
			String partKey = drawingId + "_" + batchNum;
			String opKey = drawingId + "_" + batchNum + "_"
					+ (operationIdDesc == null ? "" : operationIdDesc);
			SPartTask partTask = allPartTaskMap.get(partKey);
			if (partTask == null) {
				return null;
			}
			SOperationTask opTask = allOperationTaskMap.get(opKey);
			// 写日志
			Element taskElement = new Element("PARTTASK_SPLITBATCH");
			taskElement.setAttribute("partUid", partTask.getPartUid());
			taskElement.setAttribute("drawingId", partTask.getDrawingId());
			taskElement.setAttribute("batchNum", partTask.getBatchNum());
			taskElement.setAttribute("planQty", String.valueOf(partTask
					.getPlanQty()));

			List<OperationSplitBatch> splitedBatchs = this.operationSplitBatchManager
					.findChildOperation(record);

			for (OperationSplitBatch splitBatch : splitedBatchs) {
				Element splitTaskElement = new Element("splitTask");
				splitTaskElement.setAttribute("batchNum", splitBatch
						.getNewBatchNum());
				splitTaskElement.setAttribute("planQty", String
						.valueOf(splitBatch.getSplitNum()));
				if (splitBatch.getNewBatchNum().equals(partTask.getBatchNum())) {
					splitTaskElement.setAttribute("operationIdDesc", splitBatch
							.getStartOperaton());
				} else {
					splitTaskElement.setAttribute("operationIdDesc", splitBatch
							.getStartOperaton());
				}

				taskElement.addContent(splitTaskElement);

			}
			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH)) {
			// 写日志
			Element taskElement = new Element("PARTTASK_COMBINEBATCH");
			taskElement.setAttribute("drawingId", drawingId);
			String batchNums = record.getBatchNum();

			String[] batchNumArray = batchNums.split(",");
			for (String batchNum : batchNumArray) {
				String partKey = drawingId + "_" + batchNum;
				SPartTask partTask = allPartTaskMap.get(partKey);
				Element combineTaskElement = new Element("combineTask");
				combineTaskElement.setAttribute("batchNum", partTask
						.getBatchNum());
				combineTaskElement.setAttribute("planQty", String
						.valueOf(partTask.getPlanQty()));
				combineTaskElement.setAttribute("taskUid", partTask
						.getTaskUid());
				taskElement.addContent(combineTaskElement);
			}
			return taskElement;
		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD)) {

			List<OperationPartTask> newPartTasks = this.operationPartTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("PARTTASK_ADD");
			for (OperationPartTask newPartTask : newPartTasks) {
				Element addElement = new Element("addPartTask");
				// 写日志
				addElement.setAttribute("scheduleUid", newPartTask
						.getScheduleUid());
				// taskElement.setAttribute("partUid",
				// newPartTasks.getPartUid());
				addElement
						.setAttribute("drawingId", newPartTask.getDrawingId());
				addElement.setAttribute("partName", newPartTask.getPartName());
				addElement.setAttribute("partUid", newPartTask.getPartUid());
				addElement.setAttribute("batchNum", newPartTask.getBatchNum());
				if(newPartTask.getModel()!=null){
					addElement.setAttribute("model", newPartTask.getModel());
				}else{
					addElement.setAttribute("model", "");
				}
				
				if (newPartTask.getDeptUid() != null) {
					addElement.setAttribute("deptId", newPartTask.getDeptUid());
				} else {
					addElement.setAttribute("deptId", "");
				}
				if (newPartTask.getPlanQty() != null) {
					addElement.setAttribute("planQty", String
							.valueOf(newPartTask.getPlanQty()));
				} else {
					addElement.setAttribute("planQty", "");
				}

				if (newPartTask.getCompleteQty() != null) {
					addElement.setAttribute("completeQty", String
							.valueOf(newPartTask.getCompleteQty()));
				} else {
					addElement.setAttribute("completeQty", "");
				}

				if (newPartTask.getIsCritical() != null) {
					addElement.setAttribute("isCritical", String
							.valueOf(newPartTask.getIsCritical()));
				} else {
					addElement.setAttribute("isCritical", "");
				}

				if (newPartTask.getDelayCost() != null) {
					addElement.setAttribute("delayCost", String
							.valueOf(newPartTask.getDelayCost()));
				} else {
					addElement.setAttribute("delayCost", "");
				}
				if (newPartTask.getPlanGrade() != null) {
					addElement.setAttribute("planGrade", String
							.valueOf(newPartTask.getPlanGrade()));
				} else {
					addElement.setAttribute("planGrade", "");
				}

				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (newPartTask.getEarlyStart() == null) {
					addElement.setAttribute("earlyStart", "");
				} else {
					addElement.setAttribute("earlyStart", formatDate
							.format(newPartTask.getEarlyStart()));
				}
				if (newPartTask.getLateFinish() == null) {
					addElement.setAttribute("lateFinish", "");
				} else {
					addElement.setAttribute("lateFinish", formatDate
							.format(newPartTask.getLateFinish()));
				}
				if (newPartTask.getActualStart() == null) {
					addElement.setAttribute("actualStart", "");
				} else {
					addElement.setAttribute("actualStart", formatDate
							.format(newPartTask.getActualStart()));
				}
				if (newPartTask.getActualFinish() == null) {
					addElement.setAttribute("actualFinish", "");
				} else {
					addElement.setAttribute("actualFinish", formatDate
							.format(newPartTask.getActualFinish()));
				}

				taskElement.addContent(addElement);
			}

			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_DELETE)) {

			List<OperationPartTask> deletePartTasks = this.operationPartTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("PARTTASK_DELETE");
			for (OperationPartTask deletePartTask : deletePartTasks) {
				Element addElement = new Element("deletePartTask");
				// 写日志
				// addElement.setAttribute("drawingId", drawingId);
				// taskElement.setAttribute("partUid",
				// newPartTasks.getPartUid());
				addElement.setAttribute("drawingId", deletePartTask
						.getDrawingId());
				// taskElement
				// .setAttribute("partName", newPartTask.getPartName());
				addElement.setAttribute("batchNum", deletePartTask
						.getBatchNum());
				// taskElement.setAttribute("model", newPartTask.getModel());

				addElement.setAttribute("planQty", String
						.valueOf(deletePartTask.getPlanQty()));
				addElement.setAttribute("isCritical", String
						.valueOf(deletePartTask.getIsCritical()));
				addElement.setAttribute("delayCost", String
						.valueOf(deletePartTask.getDelayCost()));
				addElement.setAttribute("planGrade", String
						.valueOf(deletePartTask.getPlanGrade()));

				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (deletePartTask.getEarlyStart() == null) {
					addElement.setAttribute("earlyStart", "");
				} else {
					addElement.setAttribute("earlyStart", formatDate
							.format(deletePartTask.getEarlyStart()));
				}
				if (deletePartTask.getLateFinish() == null) {
					addElement.setAttribute("lateFinish", "");
				} else {
					addElement.setAttribute("lateFinish", formatDate
							.format(deletePartTask.getLateFinish()));
				}

				taskElement.addContent(addElement);
			}
			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO)) {

			List<OperationPartTask> updatedPartTasks = this.operationPartTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("PARTTASK_PLANINFO");
			for (OperationPartTask updatedPartTask : updatedPartTasks) {

				Element updateElement = new Element("updatePartTask");
				// 写日志
				updateElement.setAttribute("partUid", updatedPartTask
						.getPartUid());
				updateElement.setAttribute("batchNum", updatedPartTask
						.getBatchNum());

				if (updatedPartTask.getPlanQty() != null) {
					updateElement.setAttribute("planQty", String
							.valueOf(updatedPartTask.getPlanQty()));
				} else {
					updateElement.setAttribute("planQty", "");
				}
				if (updatedPartTask.getIsCritical() != null) {
					updateElement.setAttribute("isCritical", String
							.valueOf(updatedPartTask.getIsCritical()));
				} else {
					updateElement.setAttribute("isCritical", "");
				}
				if (updatedPartTask.getDelayCost() != null) {
					updateElement.setAttribute("delayCost", String
							.valueOf(updatedPartTask.getDelayCost()));
				} else {
					updateElement.setAttribute("delayCost", "");
				}

				if (updatedPartTask.getPlanGrade() != null) {
					updateElement.setAttribute("planGrade", String
							.valueOf(updatedPartTask.getPlanGrade()));
				} else {
					updateElement.setAttribute("planGrade", "");
				}

				if (updatedPartTask.getCompleteQty() != null) {
					updateElement.setAttribute("completeQty", String
							.valueOf(updatedPartTask.getCompleteQty()));
				} else {
					updateElement.setAttribute("completeQty", "");
				}

				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (updatedPartTask.getEarlyStart() == null) {
					updateElement.setAttribute("earlyStart", "");
				} else {
					updateElement.setAttribute("earlyStart", formatDate
							.format(updatedPartTask.getEarlyStart()));
				}
				if (updatedPartTask.getLateFinish() == null) {
					updateElement.setAttribute("lateFinish", "");
				} else {
					updateElement.setAttribute("lateFinish", formatDate
							.format(updatedPartTask.getLateFinish()));
				}

				if (updatedPartTask.getActualStart() == null) {
					updateElement.setAttribute("actualStart", "");
				} else {
					updateElement.setAttribute("actualStart", formatDate
							.format(updatedPartTask.getActualStart()));
				}
				if (updatedPartTask.getActualFinish() == null) {
					updateElement.setAttribute("actualFinish", "");
				} else {
					updateElement.setAttribute("actualFinish", formatDate
							.format(updatedPartTask.getActualFinish()));
				}
				taskElement.addContent(updateElement);
			}

			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_GROUP)) {

			List<OperationGroupTasks> groupPartTasks = this.operationGroupTasksManager
					.findChildOperation(record);
			Element taskElement = new Element("OPERATIONTASK_GROUPTASKS");
			for (OperationGroupTasks groupTask : groupPartTasks) {
				Element groupElement = new Element("groupTasks");
				// 写日志
				// updateElement.setAttribute("drawingId", drawingId);
				// taskElement.setAttribute("partUid",
				// newPartTasks.getPartUid());
				groupElement
						.setAttribute("drawingId", groupTask.getDrawingId());
				// taskElement
				// .setAttribute("partName", newPartTask.getPartName());
				groupElement.setAttribute("batchNum", groupTask.getBatchNum());
				// taskElement.setAttribute("model",
				// newPartTask.getModel());
				groupElement.setAttribute("operationIdDesc", groupTask
						.getOperationIdDesc());

				taskElement.addContent(groupElement);
			}

			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_ADD)) {

			List<OperationOperationTask> operationTasks = this.operationOperationTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("OPERATIONTASK_ADD");
			for (OperationOperationTask operationTask : operationTasks) {
				Element groupElement = new Element("addOperationTask");
				// 写日志

				// groupElement
				// .setAttribute("taskUid", operationTask.getTaskUid());
				// groupElement.setAttribute("partUid",
				// operationTask.getPartUid());
				groupElement.setAttribute("drawingId", operationTask
						.getDrawingId());
				// taskElement.setAttribute("partName", so.getPartName());
				groupElement.setAttribute("batchNum", operationTask
						.getBatchNum());
				groupElement.setAttribute("operationIdDesc", operationTask
						.getOperationIdDesc());
				groupElement.setAttribute("taskName", operationTask
						.getTaskName());
				if (operationTask.getLocked() != null) {
					groupElement.setAttribute("locked", String
							.valueOf(operationTask.getLocked()));
				}
				if (operationTask.getSuccOperationStr() != null) {
					groupElement.setAttribute("succOperationStr", operationTask
							.getSuccOperationStr());
				} else {
					groupElement.setAttribute("succOperationStr", "");
				}
				groupElement.setAttribute("operationId", String
						.valueOf(operationTask.getOperationId()));
				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (operationTask.getEarlyStart() == null) {
					groupElement.setAttribute("earlyStart", "");
				} else {
					groupElement.setAttribute("earlyStart", formatDate
							.format(operationTask.getEarlyStart()));
				}
				if (operationTask.getLateFinish() == null) {
					groupElement.setAttribute("lateFinish", "");
				} else {
					groupElement.setAttribute("lateFinish", formatDate
							.format(operationTask.getLateFinish()));
				}
				groupElement.setAttribute("planQty", String
						.valueOf(operationTask.getPlanQty()));
				groupElement.setAttribute("completeQty", String
						.valueOf(operationTask.getCompleteQty()));
				groupElement.setAttribute("estiRunTime", String
						.valueOf(operationTask.getEstiRunTime()));
				groupElement.setAttribute("estiPreTime", String
						.valueOf(operationTask.getEstiPreTime()));

				taskElement.addContent(groupElement);
			}

			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_DELETE)) {

			List<OperationOperationTask> operationTasks = this.operationOperationTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("OPERATIONTASK_DELETE");
			for (OperationOperationTask operationTask : operationTasks) {
				Element groupElement = new Element("addOperationTask");
				// 写日志

				// groupElement
				// .setAttribute("taskUid", operationTask.getTaskUid());
				// groupElement.setAttribute("partUid",
				// operationTask.getPartUid());
				groupElement.setAttribute("drawingId", operationTask
						.getDrawingId());
				// taskElement.setAttribute("partName", so.getPartName());
				groupElement.setAttribute("batchNum", operationTask
						.getBatchNum());
				groupElement.setAttribute("operationIdDesc", operationTask
						.getOperationIdDesc());
				groupElement.setAttribute("taskName", operationTask
						.getTaskName());
				if (operationTask.getLocked() != null) {
					groupElement.setAttribute("locked", String
							.valueOf(operationTask.getLocked()));
				}
				if (operationTask.getSuccOperationStr() != null) {
					groupElement.setAttribute("succOperationStr", operationTask
							.getSuccOperationStr());
				} else {
					groupElement.setAttribute("succOperationStr", "");
				}
				groupElement.setAttribute("operationId", String
						.valueOf(operationTask.getOperationId()));
				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (operationTask.getEarlyStart() == null) {
					groupElement.setAttribute("earlyStart", "");
				} else {
					groupElement.setAttribute("earlyStart", formatDate
							.format(operationTask.getEarlyStart()));
				}
				if (operationTask.getLateFinish() == null) {
					groupElement.setAttribute("lateFinish", "");
				} else {
					groupElement.setAttribute("lateFinish", formatDate
							.format(operationTask.getLateFinish()));
				}
				groupElement.setAttribute("planQty", String
						.valueOf(operationTask.getPlanQty()));
				groupElement.setAttribute("completeQty", String
						.valueOf(operationTask.getCompleteQty()));
				groupElement.setAttribute("estiRunTime", String
						.valueOf(operationTask.getEstiRunTime()));
				groupElement.setAttribute("estiPreTime", String
						.valueOf(operationTask.getEstiPreTime()));

				taskElement.addContent(groupElement);
			}

			return taskElement;
		}

		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_PLANINFO)) {

			List<OperationOperationTask> operationTasks = this.operationOperationTaskManager
					.findChildOperation(record);
			Element taskElement = new Element("OPERATIONTASK_PLANINFO");
			for (OperationOperationTask operationTask : operationTasks) {
				Element groupElement = new Element("updatedOperationTask");
				// 写日志

				if (operationTask.getTaskUid() != null) {
					groupElement.setAttribute("taskUid", operationTask
							.getTaskUid());
				} else {
					groupElement.setAttribute("taskUid", "");
				}
				if (operationTask.getDrawingId() != null) {
					groupElement.setAttribute("drawingId", operationTask
							.getDrawingId());
				} else {
					groupElement.setAttribute("drawingId", "");
				}

				if (operationTask.getBatchNum() != null) {
					groupElement.setAttribute("batchNum", operationTask
							.getBatchNum());
				} else {
					groupElement.setAttribute("batchNum", "");
				}
				// taskElement.setAttribute("partName", so.getPartName());
				if (operationTask.getOperationIdDesc() != null) {
					groupElement.setAttribute("operationIdDesc", operationTask
							.getOperationIdDesc());
				} else {
					groupElement.setAttribute("operationIdDesc", operationTask
							.getOperationIdDesc());
				}

				if (operationTask.getTaskName() != null) {
					groupElement.setAttribute("taskName", operationTask
							.getTaskName());
				} else {
					groupElement.setAttribute("taskName", "");
				}

				if (operationTask.getLocked() != null) {
					groupElement.setAttribute("locked", String
							.valueOf(operationTask.getLocked()));
				}
				if (operationTask.getSuccOperationStr() != null) {
					groupElement.setAttribute("succOperationStr", operationTask
							.getSuccOperationStr());
				} else {
					groupElement.setAttribute("succOperationStr", "");
				}

				if (operationTask.getOperationId() != null) {
					groupElement.setAttribute("operationId", String
							.valueOf(operationTask.getOperationId()));
				} else {
					groupElement.setAttribute("operationId", "");
				}

				if (operationTask.getMasterShop() != null) {
					groupElement.setAttribute("masterShop", String
							.valueOf(operationTask.getMasterShop()));
				} else {
					groupElement.setAttribute("masterShop", "");
				}
				if (operationTask.getMasterShopName() != null) {
					groupElement.setAttribute("masterShopName", String
							.valueOf(operationTask.getMasterShopName()));
				} else {
					groupElement.setAttribute("masterShopName", "");
				}
				if (operationTask.getWorkCenterUid() != null) {
					groupElement.setAttribute("workCenterUid", String
							.valueOf(operationTask.getWorkCenterUid()));
				} else {
					groupElement.setAttribute("workCenterUid", "");
				}
				if (operationTask.getWorkCenterName() != null) {
					groupElement.setAttribute("workCenterName", String
							.valueOf(operationTask.getWorkCenterName()));
				} else {
					groupElement.setAttribute("workCenterName", "");
				}

				if (operationTask.getCalUid() != null) {
					groupElement.setAttribute("calUid", String
							.valueOf(operationTask.getCalUid()));
				} else {
					groupElement.setAttribute("calUid", "");
				}

				if (operationTask.getCalName() != null) {
					groupElement.setAttribute("calName", String
							.valueOf(operationTask.getCalName()));
				} else {
					groupElement.setAttribute("calName", "");
				}

				if (operationTask.getDeptId() != null) {
					groupElement.setAttribute("deptId", String
							.valueOf(operationTask.getDeptId()));
				} else {
					groupElement.setAttribute("deptId", "");
				}

				if (operationTask.getDeviceSeq() != null) {
					groupElement.setAttribute("deviceSeq", String
							.valueOf(operationTask.getDeviceSeq()));
				} else {
					groupElement.setAttribute("deviceSeq", "");
				}

				SimpleDateFormat formatDate = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				if (operationTask.getEarlyStart() == null) {
					groupElement.setAttribute("earlyStart", "");
				} else {
					groupElement.setAttribute("earlyStart", formatDate
							.format(operationTask.getEarlyStart()));
				}
				if (operationTask.getLateFinish() == null) {
					groupElement.setAttribute("lateFinish", "");
				} else {
					groupElement.setAttribute("lateFinish", formatDate
							.format(operationTask.getLateFinish()));
				}
				if (operationTask.getBaseStart() == null) {
					groupElement.setAttribute("baseStart", "");
				} else {
					groupElement.setAttribute("baseStart", formatDate
							.format(operationTask.getBaseStart()));
				}
				if (operationTask.getBaseFinish() == null) {
					groupElement.setAttribute("baseFinish", "");
				} else {
					groupElement.setAttribute("baseFinish", formatDate
							.format(operationTask.getBaseFinish()));
				}

				if (operationTask.getPlanQty() != null) {
					groupElement.setAttribute("planQty", String
							.valueOf(operationTask.getPlanQty()));
				} else {
					groupElement.setAttribute("planQty", String
							.valueOf(operationTask.getPlanQty()));
				}

				if (operationTask.getCompleteQty() != null) {
					groupElement.setAttribute("completeQty", String
							.valueOf(operationTask.getCompleteQty()));
				} else {
					groupElement.setAttribute("completeQty", String
							.valueOf(operationTask.getCompleteQty()));
				}

				if (operationTask.getEstiRunTime() != null) {
					groupElement.setAttribute("estiRunTime", String
							.valueOf(operationTask.getEstiRunTime()));
				} else {
					groupElement.setAttribute("estiRunTime", String
							.valueOf(operationTask.getEstiRunTime()));
				}
				if (operationTask.getEstiPreTime() != null) {
					groupElement.setAttribute("estiPreTime", String
							.valueOf(operationTask.getEstiPreTime()));
				} else {
					groupElement.setAttribute("estiPreTime", String
							.valueOf(operationTask.getEstiPreTime()));
				}

				taskElement.addContent(groupElement);
			}

			return taskElement;
		}

		return null;

	}

	 
	public void copyOperationRecord(ScheduleScheme scheduleScheme) throws Exception {

		// TODO Auto-generated method stub
		if (scheduleScheme.getBaseScheduleUid() == null) {
			return;
		}
		List<ScheduleOperationRecord> recordLists = new ArrayList<ScheduleOperationRecord>();
		recordLists = scheduleOperationRecordDao.findByProperty("scheduleUid", scheduleScheme
				.getBaseScheduleUid());
		if (recordLists == null || recordLists.size() == 0) {
			return;
		}
		for (ScheduleOperationRecord record : recordLists) {
			ScheduleOperationRecord newRecord = new ScheduleOperationRecord();
			// 这个地方要考虑操作的父子关系，可能要修改数据库的主键
			ObjectOperation.copyAttributes(record, newRecord);
			newRecord.setScheduleUid(scheduleScheme.getScheduleUid());
			newRecord.setEventState(1);
			// newRecord.setCreateTime(new Date());
			List<ScheduleOperationRecord> childRecordsList = this.scheduleOperationRecordDao
					.findChildRecord(record);

			this.add(newRecord);
			// for (ScheduleOperationRecord childRecord : childRecordsList) {
			// if (recordLists.contains(childRecord)) {
			// ScheduleOperationRecord rRecord = recordLists
			// .get(recordLists.indexOf(childRecord));
			// rRecord.setParentUid(newRecord.getScheduleOperationUid());
			// this.update(rRecord);
			// log.debug("aaaaaaa");
			// }
			// }
			this.copyChildOperation(record, newRecord);

		}

	}

	private void copyChildOperation(ScheduleOperationRecord record,
			ScheduleOperationRecord newRecord) throws Exception {

		// TODO Auto-generated method stub
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD)
				|| record.getEventType().equals(
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_DELETE)
				|| record.getEventType().equals(
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO)) {
			List<OperationPartTask> operationTaskList = this.operationPartTaskManager
					.findChildOperation(record);
			if (operationTaskList == null || operationTaskList.size() == 0) {
				return;
			}
			for (OperationPartTask op : operationTaskList) {
				OperationPartTask newOp = new OperationPartTask();
				ObjectOperation.copyAttributes(op, newOp);
				newOp.setScheduleOperationUid(IdentityGenerator
						.GetIdentityString());
				newOp.setScheduleUid(newRecord.getScheduleUid());
				newOp.setParentUniqueId(newRecord.getScheduleOperationUid());
				this.operationPartTaskManager.add(newOp);
			}

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_DELETE)) {
		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO)) {
		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH)) {
			List<OperationSplitBatch> operationTaskList = this.operationSplitBatchManager
					.findChildOperation(record);
			if (operationTaskList == null || operationTaskList.size() == 0) {
				return;
			}
			for (OperationSplitBatch op : operationTaskList) {
				OperationSplitBatch newOp = new OperationSplitBatch();
				ObjectOperation.copyAttributes(op, newOp);
				newOp.setScheduleOperationUid(IdentityGenerator
						.GetIdentityString());
				newOp.setScheduleUid(newRecord.getScheduleUid());
				newOp.setParentUniqueId(newRecord.getScheduleOperationUid());
				this.operationSplitBatchManager.add(newOp);
			}

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH)) {
			return;

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_ADD)
				|| record.getEventType().equals(
						ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_DELETE)
				|| record
						.getEventType()
						.equals(
								ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_PLANINFO)) {
			List<OperationOperationTask> operationTaskList = this.operationOperationTaskManager
					.findChildOperation(record);
			if (operationTaskList == null || operationTaskList.size() == 0) {
				return;
			}
			for (OperationOperationTask op : operationTaskList) {
				OperationOperationTask newOp = new OperationOperationTask();
				ObjectOperation.copyAttributes(op, newOp);
				newOp.setScheduleOperationUid(IdentityGenerator
						.GetIdentityString());
				newOp.setScheduleUid(newRecord.getScheduleUid());
				newOp.setParentUniqueId(newRecord.getScheduleOperationUid());
				this.operationOperationTaskManager.add(newOp);
			}

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_DELETE)) {

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_PLANINFO)) {

		}
		if (record.getEventType().equals(
				ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_GROUP)) {
			List<OperationGroupTasks> operationTaskList = this.operationGroupTasksManager
					.findChildOperation(record);
			if (operationTaskList == null || operationTaskList.size() == 0) {
				return;
			}
			for (OperationGroupTasks op : operationTaskList) {
				OperationGroupTasks newOp = new OperationGroupTasks();
				ObjectOperation.copyAttributes(op, newOp);
				newOp.setScheduleOperationUid(IdentityGenerator
						.GetIdentityString());
				newOp.setScheduleUid(newRecord.getScheduleUid());
				newOp.setParentUniqueId(newRecord.getScheduleOperationUid());
				this.operationGroupTasksManager.add(newOp);
			}

		}

	}

	 
	public void writeOperationPartTaskLog(ScheduleScheme scheme,
			SPartTask oldPartTask, SPartTask partTask, String eventType) throws Exception {

		// TODO Auto-generated method stub
		// 先将已经存在的类似事件清除掉，以达到只保留最后操作的效果
		String parentUid = "";
		List<ScheduleOperationRecord> createdList = new ArrayList<ScheduleOperationRecord>();
		createdList = this.scheduleOperationRecordDao.findAllByConditions(
				scheme, oldPartTask, eventType);
		// 已经存在操作的父操作
		List<ScheduleOperationRecord> parentList = new ArrayList<ScheduleOperationRecord>();
		// 已经存在操作的子操作
		List<ScheduleOperationRecord> childList = new ArrayList<ScheduleOperationRecord>();
		// 如果存在类似的操作了，则只更新
		if (createdList != null && createdList.size() > 0) {
			// 清除不是简单的删除，还要建立父子操作的关系
			// this.delete(createdList);
			for (ScheduleOperationRecord createdRecord : createdList) {
				createdRecord.setPlanQty(String.valueOf(partTask.getPlanQty()));
				createdRecord.setCreateTime(new Date());
				List<OperationPartTask> partTaskList = this.operationPartTaskManager
						.findChildOperation(createdRecord);
				if (partTaskList != null && partTaskList.size() > 0) {
					for (OperationPartTask opPartTask : partTaskList) {
						opPartTask.setEarlyStart(partTask.getEarlyStart());
						opPartTask.setLateFinish(partTask.getLateFinish());
						opPartTask.setPlanQty(partTask.getPlanQty());
						opPartTask.setIsCritical(partTask.getIsCritical());
						opPartTask.setDelayCost(partTask.getDelayCost());
						opPartTask.setPlanGrade(partTask.getPlanGrade());
					}
					this.operationPartTaskManager.update(partTaskList);
				}
				this.update(createdList);
			}
		} else {
			// 不存在就新建
			ScheduleOperationRecord operationRecord = new ScheduleOperationRecord();
			operationRecord.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			operationRecord.setDrawingId(partTask.getDrawingId());
			operationRecord.setBatchNum(partTask.getBatchNum());
			operationRecord.setPlanQty(String.valueOf(partTask.getPlanQty()));
			operationRecord.setCreateTime(new Date());
			operationRecord.setScheduleUid(scheme.getScheduleUid());
			OperationPartTask opPartTask = new OperationPartTask();
			opPartTask.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			opPartTask.setPartUid(partTask.getPartUid());
			opPartTask.setPartName(partTask.getPartName());
			opPartTask.setModel(partTask.getModel());
			opPartTask.setDrawingId(partTask.getDrawingId());
			opPartTask.setBatchNum(partTask.getBatchNum());
			opPartTask.setParentUniqueId(operationRecord
					.getScheduleOperationUid());
			opPartTask.setScheduleUid(scheme.getScheduleUid());
			opPartTask.setEarlyStart(partTask.getEarlyStart());
			opPartTask.setLateFinish(partTask.getLateFinish());
			opPartTask.setPlanQty(partTask.getPlanQty());
			opPartTask.setIsCritical(partTask.getIsCritical());
			opPartTask.setDelayCost(partTask.getDelayCost());
			opPartTask.setPlanGrade(partTask.getPlanGrade());
			// 更新的话要比较不同
			if (eventType
					.equals(ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO)) {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO);
				// 更新时要判断是否是新建、分批或合批来的，是则挂在新建下面

				List<ScheduleOperationRecord> addedList = this.scheduleOperationRecordDao
						.findAllByConditions(scheme, oldPartTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
				// 分批的情况要考虑操作的详细信息
				List<OperationSplitBatch> opSplitList = new ArrayList<OperationSplitBatch>();
				opSplitList = this.operationSplitBatchManager
						.findSplitOperation(partTask.getDrawingId(), partTask
								.getBatchNum(), scheme.getScheduleUid());
				List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
						.findAllByConditions(
								scheme,
								oldPartTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
				if (addedList != null && addedList.size() > 0) {
					parentUid = addedList.get(0).getScheduleOperationUid();
				}

				if (opSplitList != null && opSplitList.size() > 0) {
					// 先通过详细操作记录中找出
					for (OperationSplitBatch oper : opSplitList) {
						if (oper.getDrawingId().equals(partTask.getDrawingId())
								&& oper.getNewBatchNum().equals(
										partTask.getBatchNum())) {
							ScheduleOperationRecord record = new ScheduleOperationRecord();
							record.setScheduleUid(oper.getScheduleUid());
							record.setParentUid(oper.getParentUniqueId());
							List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
									.findParentRecord(record);
							if (splitedList != null && splitedList.size() > 0) {
								parentUid = splitedList.get(0)
										.getScheduleOperationUid();
								break;
							}
						}
					}
				}
				if (combinedList != null && combinedList.size() > 0) {
					parentUid = combinedList.get(0).getScheduleOperationUid();
				}
				operationRecord.setParentUid(parentUid);

			}
			// 添加的话要记录所有有用信息
			else if (eventType
					.equals(ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD)) {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
				List<ScheduleOperationRecord> deletedList = new ArrayList<ScheduleOperationRecord>();
				deletedList = this.scheduleOperationRecordDao
						.findAllByConditions(
								scheme,
								oldPartTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_DELETE);
				// 添加时存在删除事件且任务信息相同（所有信息），则直接将添加事件删除，表明添加事件无效
				if (deletedList != null && deletedList.size() > 0) {
					this.deleteRecords(deletedList);
					return;
				}
			} else {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_DELETE);
				List<ScheduleOperationRecord> addedList = new ArrayList<ScheduleOperationRecord>();
				// 删除时要判断是否是分批或合批来的，是则挂在新建下面

				// 分批的情况要考虑操作的详细信息
				// List<ScheduleOperationRecord> splitedList =
				// this.scheduleOperationRecordDao
				// .findAllByConditions(
				// scheme,
				// oldPartTask,
				// ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH);
				List<OperationSplitBatch> opSplitList = new ArrayList<OperationSplitBatch>();
				opSplitList = this.operationSplitBatchManager
						.findSplitOperation(partTask.getDrawingId(), partTask
								.getBatchNum(), scheme.getScheduleUid());
				List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
						.findAllByConditions(
								scheme,
								oldPartTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);

				if (opSplitList != null && opSplitList.size() > 0) {
					// 先通过详细操作记录中找出
					for (OperationSplitBatch oper : opSplitList) {
						if (oper.getDrawingId().equals(partTask.getDrawingId())
								&& oper.getNewBatchNum().equals(
										partTask.getBatchNum())) {
							ScheduleOperationRecord record = new ScheduleOperationRecord();
							record.setScheduleUid(oper.getScheduleUid());
							record.setParentUid(oper.getParentUniqueId());
							List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
									.findParentRecord(record);
							if (splitedList != null && splitedList.size() > 0) {
								parentUid = splitedList.get(0)
										.getScheduleOperationUid();
								break;
							}
						}
					}
				}

				// if (splitedList != null && splitedList.size() > 0) {
				// // List<OperationSplitBatch> opSplitList = new
				// // ArrayList<OperationSplitBatch>();
				// opSplitList = this.operationSplitBatchManager
				// .findChildOperation(splitedList.get(0));
				// for (OperationSplitBatch oper : opSplitList) {
				// if (oper.getDrawingId().equals(
				// oldPartTask.getDrawingId())
				// && oper.getNewBatchNum().equals(
				// oldPartTask.getBatchNum())) {
				// parentUid = splitedList.get(0)
				// .getScheduleOperationUid();
				// break;
				// }
				// }
				// }
				if (combinedList != null && combinedList.size() > 0) {
					parentUid = combinedList.get(0).getScheduleOperationUid();
				}
				addedList = this.scheduleOperationRecordDao
						.findAllByConditions(scheme, oldPartTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
				// 删除时存在添加事件且任务信息相同（图号、批次），则直接将添加事件删除，表明添加事件无效
				if (addedList != null && addedList.size() > 0) {
					this.deleteRecords(addedList);
					return;
				}
			}
			operationRecord.setParentUid(parentUid);
			operationRecord.setEventState(2);
			this.add(operationRecord);
			this.operationPartTaskManager.add(opPartTask);
		}

	}

	/**
	 * 删除某一操作时应该也要删除对应的子操作
	 * 
	 * @param deleteedList
	 * @throws Exception 
	 */
	private void deleteRecords(List<ScheduleOperationRecord> deleteedList) throws Exception {
		if (deleteedList == null && deleteedList.size() == 0) {
			return;
		}
		for (ScheduleOperationRecord record : deleteedList) {
			List<ScheduleOperationRecord> childList = this.scheduleOperationRecordDao
					.findChildRecord(record);
			deleteRecords(childList);
			this.delete(record);

		}

	}

	 
	public void writeCombineBatchLog(ScheduleScheme scheme,
			List<SPartTask> combineBatchList) throws Exception {

		// TODO Auto-generated method stub
		String parentUid = "";
		ScheduleOperationRecord operationRecord = new ScheduleOperationRecord();
		operationRecord.setScheduleOperationUid(IdentityGenerator
				.GetIdentityString());
		operationRecord.setDrawingId(combineBatchList.get(0).getDrawingId());
		String batchNum = "";
		String planQty = "";
		for (SPartTask partTask : combineBatchList) {
			batchNum += partTask.getBatchNum() + ",";
			planQty += partTask.getPlanQty() + ",";
		}
		batchNum = batchNum.substring(0, batchNum.length() - 1);
		planQty = planQty.substring(0, planQty.length() - 1);
		operationRecord.setBatchNum(batchNum);
		// operationRecord.setPlanQty(String.valueOf(partTask.getPlanQty()));
		operationRecord.setCreateTime(new Date());
		operationRecord.setScheduleUid(scheme.getScheduleUid());
		operationRecord.setPlanQty(planQty);
		operationRecord
				.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
		operationRecord.setEventState(2);
		// 存放不同批次下不同来源
		List<ScheduleOperationRecord> sourceList = new ArrayList<ScheduleOperationRecord>();
		// 合批的挂靠可能复杂一些，要同时判断多个批次的不同情况
		for (SPartTask partTask : combineBatchList) {
			// 合批时要判断是否是新建、更新、分批或合批来的
			List<ScheduleOperationRecord> addedList = this.scheduleOperationRecordDao
					.findAllByConditions(scheme, partTask,
							ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
			List<ScheduleOperationRecord> updatedList = this.scheduleOperationRecordDao
					.findAllByConditions(scheme, partTask,
							ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO);
			// 分批的情况要考虑操作的详细信息
			List<OperationSplitBatch> opSplitList = new ArrayList<OperationSplitBatch>();
			opSplitList = this.operationSplitBatchManager.findSplitOperation(
					partTask.getDrawingId(), partTask.getBatchNum(), scheme
							.getScheduleUid());
			List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
					.findAllByConditions(
							scheme,
							partTask,
							ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
			if (addedList != null && addedList.size() > 0) {
				sourceList.addAll(addedList);
			}
			if (updatedList != null && updatedList.size() > 0) {
				sourceList.addAll(updatedList);
			}
			if (opSplitList != null && opSplitList.size() > 0) {
				// 先通过详细操作记录中找出
				for (OperationSplitBatch oper : opSplitList) {
					if (oper.getDrawingId().equals(partTask.getDrawingId())
							&& oper.getNewBatchNum().equals(
									partTask.getBatchNum())) {
						ScheduleOperationRecord record = new ScheduleOperationRecord();
						record.setScheduleUid(oper.getScheduleUid());
						record.setParentUid(oper.getParentUniqueId());
						List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
								.findParentRecord(record);
						if (splitedList != null && splitedList.size() > 0) {
							sourceList.addAll(splitedList);
						}
					}
				}
			}
			if (combinedList != null && combinedList.size() > 0) {
				sourceList.addAll(combinedList);
			}
			if (sourceList != null && sourceList.size() > 0) {
				Collections.sort(sourceList,
						new Comparator<ScheduleOperationRecord>() {

							 
							public int compare(ScheduleOperationRecord o1,
									ScheduleOperationRecord o2) {
								// TODO Auto-generated method stub
								if (o1.getCreateTime().before(
										o2.getCreateTime()))
									return 1;
								if (o1.getCreateTime()
										.after(o2.getCreateTime()))
									return -1;
								return 0;
							}
						});
				parentUid = sourceList.get(0).getScheduleOperationUid();
				operationRecord.setParentUid(parentUid);
			}
		}
		this.add(operationRecord);
	}

	 
	public void writeCancelCombineBatchLog(ScheduleScheme scheme,
			SPartTask partTask) throws Exception {

		// TODO Auto-generated method stub
		List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, partTask,
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
		this.deleteRecords(combinedList);
	}

	 
	public void writeSplitBatchLog(ScheduleScheme scheme, SPartTask partTask,
			List<SOperationTask> splitedBatchs) throws Exception {
		// TODO Auto-generated method stub
		String parentUid = "";
		String planQty = "";
		ScheduleOperationRecord operationRecord = new ScheduleOperationRecord();
		operationRecord.setScheduleOperationUid(IdentityGenerator
				.GetIdentityString());
		operationRecord.setDrawingId(partTask.getDrawingId());
		operationRecord.setBatchNum(partTask.getBatchNum());

		operationRecord.setCreateTime(new Date());
		operationRecord.setScheduleUid(scheme.getScheduleUid());
		operationRecord
				.setEventType(ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH);
		for (SOperationTask opTask : splitedBatchs) {
			planQty += opTask.getPlanQty() + ",";
		}
		planQty = planQty.substring(0, planQty.length() - 1);
		operationRecord.setPlanQty(planQty);

		// 分批时要判断是否是新建、更新、分批或合批来的，是则挂在新建下面
		List<ScheduleOperationRecord> addedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, partTask,
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
		List<ScheduleOperationRecord> updatedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, partTask,
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_PLANINFO);

		// 分批的情况要考虑操作的详细信息
		List<OperationSplitBatch> opSplitList = new ArrayList<OperationSplitBatch>();
		opSplitList = this.operationSplitBatchManager.findSplitOperation(
				partTask.getDrawingId(), partTask.getBatchNum(), scheme
						.getScheduleUid());
		List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, partTask,
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
		if (addedList != null && addedList.size() > 0) {
			parentUid = addedList.get(0).getScheduleOperationUid();
		}
		if (updatedList != null && updatedList.size() > 0) {
			parentUid = updatedList.get(0).getScheduleOperationUid();
		}

		if (opSplitList != null && opSplitList.size() > 0) {
			// 先通过详细操作记录中找出
			for (OperationSplitBatch oper : opSplitList) {
				if (oper.getDrawingId().equals(partTask.getDrawingId())
						&& oper.getNewBatchNum().equals(partTask.getBatchNum())) {
					ScheduleOperationRecord record = new ScheduleOperationRecord();
					record.setScheduleUid(oper.getScheduleUid());
					record.setParentUid(oper.getParentUniqueId());
					List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
							.findParentRecord(record);
					if (splitedList != null && splitedList.size() > 0) {
						parentUid = splitedList.get(0)
								.getScheduleOperationUid();
						break;
					}
				}
			}
		}
		if (combinedList != null && combinedList.size() > 0) {
			parentUid = combinedList.get(0).getScheduleOperationUid();
		}
		operationRecord.setParentUid(parentUid);

		operationRecord.setEventState(2);
		this.add(operationRecord);
		for (SOperationTask opTask : splitedBatchs) {
			OperationSplitBatch opSplitBatch = new OperationSplitBatch();
			opSplitBatch.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			opSplitBatch.setParentUniqueId(operationRecord
					.getScheduleOperationUid());
			opSplitBatch.setScheduleUid(scheme.getScheduleUid());
			opSplitBatch.setDrawingId(opTask.getDrawingId());
			opSplitBatch.setOriginalBatchNum(operationRecord.getBatchNum());
			opSplitBatch.setNewBatchNum(opTask.getBatchNum());
			opSplitBatch.setSplitNum(opTask.getPlanQty());
			opSplitBatch.setStartOperaton(opTask.getOperationIdDesc());
			this.operationSplitBatchManager.add(opSplitBatch);
		}

	}

	 
	public void writeCancelSplitBatchLog(ScheduleScheme scheme,
			SPartTask partTask, List<SPartTask> splitedBatchs) throws Exception {

		// TODO Auto-generated method stub
		// 取消分批时先查找分批操作，存在则删除,注意分批生成的批次必须和提交的批次相同（校验暂时没做）
		String parentUid = "";
		List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, partTask,
						ScheduleOperationRecord.EVENTTYPE_PARTTASK_SPLITBATCH);
		this.deleteRecords(splitedList);

	}

	 
	public void writeOperOperationLog(ScheduleScheme scheme,
			SOperationTask oldOpTask, SOperationTask newOpTask, String eventType) throws Exception {
		// TODO Auto-generated method stub
		String parentUid = "";
		// 先将已经存在的类似事件清除掉，以达到只保留最后操作的效果
		List<ScheduleOperationRecord> createdList = new ArrayList<ScheduleOperationRecord>();
		createdList = this.scheduleOperationRecordDao.findAllByConditions(
				scheme, oldOpTask, eventType);
		// 已经存在操作的父操作
		List<ScheduleOperationRecord> parentList = new ArrayList<ScheduleOperationRecord>();
		// 已经存在操作的子操作
		List<ScheduleOperationRecord> childList = new ArrayList<ScheduleOperationRecord>();
		// 如果存在类似的操作了，则只更新
		if (createdList != null && createdList.size() > 0) {
			// 清除不是简单的删除，还要建立父子操作的关系
			// this.delete(createdList);
			for (ScheduleOperationRecord createdRecord : createdList) {
				createdRecord
						.setPlanQty(String.valueOf(newOpTask.getPlanQty()));
				createdRecord.setCreateTime(new Date());
				List<OperationOperationTask> partTaskList = this.operationOperationTaskManager
						.findChildOperation(createdRecord);
				if (partTaskList != null && partTaskList.size() > 0) {
					for (OperationOperationTask opTask : partTaskList) {
						opTask.setEarlyStart(newOpTask.getEarlyStart());
						opTask.setLateFinish(newOpTask.getLateFinish());
						opTask.setPlanQty(newOpTask.getPlanQty());
						opTask.setCompleteQty(newOpTask.getCompleteQty());
						opTask.setSuccOperationStr(newOpTask
								.getSuccOperationStr());
						opTask.setLocked(newOpTask.getLocked());
						opTask.setDeptId(newOpTask.getDeptUid());
						opTask.setDeviceSeq(newOpTask.getDeviceSeq());
						opTask.setMasterShop(newOpTask.getMasterShop());
						opTask.setMasterShopName(newOpTask.getMasterShopName());
						opTask.setWorkCenterUid(newOpTask.getDeviceClass());
						opTask
								.setWorkCenterName(newOpTask
										.getDeviceClassName());
						opTask.setCalUid(newOpTask.getCalUid());
						opTask.setEstiPreTime(newOpTask.getEstiPreTime());
						opTask.setEstiRunTime(newOpTask.getEstiRunTime());
					}
					this.operationOperationTaskManager.update(partTaskList);
				}
				this.update(createdList);
				// parentList = this.scheduleOperationRecordDao
				// .findParentRecord(createdRecord);
				// childList = this.scheduleOperationRecordDao
				// .findChildRecord(createdRecord);
			}
			// return;
		} else {
			ScheduleOperationRecord operationRecord = new ScheduleOperationRecord();
			operationRecord.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			operationRecord.setDrawingId(newOpTask.getDrawingId());
			operationRecord.setBatchNum(newOpTask.getBatchNum());
			operationRecord.setOperationIdDesc(newOpTask.getOperationIdDesc());
			operationRecord.setPlanQty(String.valueOf(newOpTask.getPlanQty()));
			operationRecord.setCreateTime(new Date());
			operationRecord.setScheduleUid(scheme.getScheduleUid());

			OperationOperationTask opTask = new OperationOperationTask();
			opTask.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			opTask.setPartUid(newOpTask.getPartUid());
			opTask.setDrawingId(newOpTask.getDrawingId());
			opTask.setBatchNum(newOpTask.getBatchNum());
			opTask.setOperationIdDesc(newOpTask.getOperationIdDesc());
			opTask.setTaskName(newOpTask.getTaskName());
			opTask.setParentUniqueId(operationRecord.getScheduleOperationUid());
			opTask.setScheduleUid(scheme.getScheduleUid());

			// 更新的话要比较不同
			if (eventType
					.equals(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_PLANINFO)) {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_PLANINFO);
				// 更新时要判断是否是新建、分批或合批来的，是则挂在新建下面

				SPartTask partTask = newOpTask.getParentTask();
				partTask.setBatchNum(newOpTask.getBatchNum());
				List<ScheduleOperationRecord> addedList = this.scheduleOperationRecordDao
						.findAllByConditions(scheme, partTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_ADD);
				// 分批的情况要考虑操作的详细信息
				List<OperationSplitBatch> opSplitList = new ArrayList<OperationSplitBatch>();
				opSplitList = this.operationSplitBatchManager
						.findSplitOperation(partTask.getDrawingId(), partTask
								.getBatchNum(), scheme.getScheduleUid());
				List<ScheduleOperationRecord> combinedList = this.scheduleOperationRecordDao
						.findAllByConditions(
								scheme,
								partTask,
								ScheduleOperationRecord.EVENTTYPE_PARTTASK_COMBINEBATCH);
				if (addedList != null && addedList.size() > 0) {
					parentUid = addedList.get(0).getScheduleOperationUid();
				}

				if (opSplitList != null && opSplitList.size() > 0) {
					// 先通过详细操作记录中找出
					for (OperationSplitBatch oper : opSplitList) {
						if (oper.getDrawingId().equals(partTask.getDrawingId())
								&& oper.getNewBatchNum().equals(
										partTask.getBatchNum())) {
							ScheduleOperationRecord record = new ScheduleOperationRecord();
							record.setScheduleUid(oper.getScheduleUid());
							record.setParentUid(oper.getParentUniqueId());
							List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
									.findParentRecord(record);
							if (splitedList != null && splitedList.size() > 0) {
								parentUid = splitedList.get(0)
										.getScheduleOperationUid();
								break;
							}
						}
					}
				}
				if (combinedList != null && combinedList.size() > 0) {
					parentUid = combinedList.get(0).getScheduleOperationUid();
				}
				operationRecord.setParentUid(parentUid);
			}

			// 添加的话要记录所有有用信息
			else if (eventType
					.equals(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_ADD)) {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_ADD);

			} else {
				operationRecord
						.setEventType(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_DELETE);
			}

			opTask.setEarlyStart(newOpTask.getEarlyStart());
			opTask.setLateFinish(newOpTask.getLateFinish());
			opTask.setPlanQty(newOpTask.getPlanQty());
			opTask.setCompleteQty(newOpTask.getCompleteQty());
			opTask.setSuccOperationStr(newOpTask.getSuccOperationStr());
			opTask.setLocked(newOpTask.getLocked());
			opTask.setDeptId(newOpTask.getDeptUid());
			opTask.setDeviceSeq(newOpTask.getDeviceSeq());
			opTask.setMasterShop(newOpTask.getMasterShop());
			opTask.setMasterShopName(newOpTask.getMasterShopName());
			opTask.setWorkCenterUid(newOpTask.getDeviceClass());
			opTask.setWorkCenterName(newOpTask.getDeviceClassName());
			opTask.setCalUid(newOpTask.getCalUid());
			opTask.setEstiPreTime(newOpTask.getEstiPreTime());
			opTask.setEstiRunTime(newOpTask.getEstiRunTime());
			operationRecord.setEventState(2);
			this.add(operationRecord);
			this.operationOperationTaskManager.add(opTask);
		}

	}

	 
	public void writeGroupTasksLog(ScheduleScheme scheme,
			List<SOperationTask> taskList) throws Exception {

		// TODO Auto-generated method stub
		if (taskList == null || taskList.size() == 0) {
			return;
		}
		ScheduleOperationRecord operationRecord = new ScheduleOperationRecord();
		operationRecord.setScheduleOperationUid(IdentityGenerator
				.GetIdentityString());
		operationRecord.setCreateTime(new Date());
		operationRecord.setScheduleUid(scheme.getScheduleUid());
		operationRecord
				.setEventType(ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_GROUP);
		String drawingId = "";
		String batchNum = "";
		String planQty = "";
		String operationIdDesc = "";
		for (SOperationTask opTask : taskList) {
			drawingId += opTask.getPartUid() + ",";
			batchNum += opTask.getBatchNum() + ",";
			planQty += opTask.getPlanQty() + ",";
			operationIdDesc += opTask.getOperationIdDesc() + ",";
		}

		drawingId = drawingId.substring(0, drawingId.length() - 1);
		batchNum = batchNum.substring(0, batchNum.length() - 1);
		planQty = planQty.substring(0, planQty.length() - 1);
		operationIdDesc = operationIdDesc.substring(0,
				operationIdDesc.length() - 1);
		operationRecord.setDrawingId(drawingId);
		operationRecord.setBatchNum(batchNum);
		operationRecord.setPlanQty(planQty);
		operationRecord.setOperationIdDesc(operationIdDesc);

		operationRecord.setEventState(2);
		this.add(operationRecord);
		List<OperationGroupTasks> groupList = new ArrayList<OperationGroupTasks>();
		for (SOperationTask opTask : taskList) {
			OperationGroupTasks opGroupTask = new OperationGroupTasks();
			opGroupTask.setScheduleOperationUid(IdentityGenerator
					.GetIdentityString());
			opGroupTask.setParentUniqueId(operationRecord
					.getScheduleOperationUid());
			// opGroupTask.setScheduleOperationUid(scheme.getScheduleUid());
			opGroupTask.setDrawingId(opTask.getPartUid());
			opGroupTask.setBatchNum(opTask.getBatchNum());
			opGroupTask.setOperationIdDesc(opTask.getOperationIdDesc());
			opGroupTask.setScheduleUid(scheme.getScheduleUid());
			groupList.add(opGroupTask);

		}
		this.operationGroupTasksManager.add(groupList);

	}

	 
	public void writeCancelGroupTasksLog(ScheduleScheme scheme,
			SOperationTask recordOp) throws Exception {// TODO Auto-generated method stub
		// 取消分批时先查找分批操作，存在则删除,注意分批生成的批次必须和提交的批次相同（校验暂时没做）

		List<ScheduleOperationRecord> splitedList = this.scheduleOperationRecordDao
				.findAllByConditions(scheme, recordOp,
						ScheduleOperationRecord.EVENTTYPE_OPERATIONTASK_GROUP);
		this.deleteRecords(splitedList);
	}




}
